package itesm.coler.service;

import java.util.ArrayList;

import itesm.coler.models.Autenticator;
import itesm.coler.models.Session;
import itesm.coler.models.User;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import itesm.coler.data.DAOFactory;
import itesm.coler.data.SessionDAO;
import itesm.coler.data.UserDAO;

@Produces({"application/xml", "application/json"})
@Path("/sessions")
public class SessionResource {
	
	DAOFactory MySQLFactory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
	// Create a DAO
	SessionDAO sessionDAO = MySQLFactory.getSessionDAO();

	 @GET
	 public ArrayList<Session> getSessions() {
	    ArrayList<Session> sessions = null;
	    sessions = (ArrayList<Session>) sessionDAO.findAll();
	    return sessions;
	 }
	 
	 @GET
	 @Path("/problem/{problem}/")
	 @Produces({ "application/xml", "application/json" })
	 public ArrayList<Session> getSessionsByProblem(@PathParam("problem") int problem) {
	    ArrayList<Session> sessions = null;
	    sessions = (ArrayList<Session>) sessionDAO.findAllByProblem(problem);
	    return sessions;
	 }
	 
	 /*
	@PUT
	  @Path("/add")
	  @Produces("text/plain")
	  @Consumes(MediaType.APPLICATION_JSON)
	  public Response addSession(Session session) {
	   try {
		   String results = null;
	       int result = sessionDAO.insertSession(session);
	       if (result != -1)
	          results = "Session saved : " + session;
	       else
	    	  results = "Session not saved: " + session;
	       return Response.status(201).entity(results).build();
	   
	   } catch (Exception sqle){
		   sqle.printStackTrace();
		   return null;
	   }
	  }
	
/*
	@PUT
	@Path("/modify")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response modifySession(Session session){
		  try {
			boolean result =sessionDAO.updateSession(session);
			System.out.println("Resultado de ModifySession ="+result);
			return Response.status(201).entity("ok").build();	
		  } catch (Exception sqle){
			   sqle.printStackTrace();
			   return null;
		   }
	}
	*/
	
}